<?php

/**
 * 账户提现
 */

namespace app\member\api;


class CashApi extends \app\member\api\MemberApi {

    /**
     * 财务记录
     */
    public function index() {

        $type = $this->data['type'];

        $urlParam['type'] = $type;
        if ($type == 1) {
            $where['A.status'] = 1;
        }
        if ($type == 2) {
            $where['A.status'] = 2;
        }
        if ($type == 3) {
            $where['A.status'] = 0;
        }

        $pageLimit = 10;
        $where['A.user_id'] = $this->userId;

        $model = target('member/PayCash');
        $count = $model->countList($where);
        $pageData = $this->pageData($count, $pageLimit, $urlParam);
        $list = $model->loadList($where, $pageData['limit'], 'create_time desc');

        if ($list) {
            $this->success('ok', [
                'data' => $list,
                'pageData' => [
                    'pageLimit' => $pageLimit,
                    'limit' => count($list),
                    'page' => $pageData['page'],
                    'totalPage' => $pageData['totalPage']
                ]
            ]);
        } else {
            $this->error('暂无更多记录', 404);
        }
    }

    /**
     * 提现申请
     */
    public function info() {
        $cashNo = $this->data['no'];
        $info = target('member/PayCash')->getWhereInfo([
            'cash_no' => $cashNo
        ]);
        if (empty($info)) {
            $this->error('该订单不存在!', 404);
        }

        $this->success('ok', $info);
    }

    /**
     * 提交申请
     */
    public function submit() {
        $cardInfo = target('member/PayCard')->getWhereInfo(['A.user_id' => $this->userId]);
        if(empty($cardInfo)) {
            $this->error('请先绑定银行卡');
            exit;
        }
        $money = intval($this->data['money']);
        if(empty($money)) {
            $this->error('请输入提现金额！');
        }
        $accountInfo = target('member/PayAccount')->getWhereInfo([
            'A.user_id' => $this->userId
        ]);
        if($money > $accountInfo['money'] || $money <= 0) {
            $this->error('可提现金额不足！');
        }
        if(empty($cardInfo)) {
            $this->error('请先添加银行卡！');
        }

        $config = target('member/MemberConfig')->getConfig();

        if($money < $config['clear_withdraw']) {
            $this->error('最少提现额度为'.$config['clear_withdraw'] . '元');
        }

        if($config['clear_num']) {
            $count = target('member/PayCash')->countList(['_sql' => 'A.status != 0 AND A.create_time >= ' .mktime(0,0,0,date('m'),1,date('Y')) . ' AND A.create_time <= ' . mktime(23,59,59,date('m'),date('t'),date('Y'))]);
            if($count >= $config['clear_num']) {
                $this->error('当月提现次已满，请于下月继续提现！');
            }
        }


        $cashNo = target('order/Order', 'service')->logNo();
        $data = [
            'cash_no' => $cashNo,
            'user_id' => $this->userId,
            'money' => $money,
            'create_time' => time(),
            'status' => 1,
            'bank' => $cardInfo['name'],
            'label' => $cardInfo['label'],
            'tax' => $config['clear_tax'],
            'account' => $cardInfo['account'],
            'account_tel' => $cardInfo['account_tel'],
            'account_name' => $cardInfo['account_name'],
            'account_bank' => $cardInfo['account_bank'],
        ];
        target('member/PayCash')->beginTransaction();
        if(!target('member/PayCash')->add($data)) {
            target('member/PayCash')->rollBack();
            $this->error('提现申请失败!');
        }
        $status = target('member/Finance', 'service')->account([
            'user_id' => $this->userId,
            'money' => $money,
            'pay_no' => $cashNo,
            'pay_name' => '账户提现',
            'type' => 0,
            'deduct' => 1
        ]);
        if(!$status) {
            target('member/PayCash')->rollBack();
            $this->error(target('member/Finance', 'service')->getError());
        }

        target('member/PayCash')->commit();
        $this->success('提现申请成功！');

    }

}